from kss.core import KSSView, kssaction
from my315ok.flowrate import read,pkopenw,write,total,close
from zope.component import getMultiAdapter
from Products.CMFCore.utils import getToolByName

class totalupdateView(KSSView):     

    @kssaction
    def updatetotal(self):
        # build HTML
        content = str(read('total'))
        try:
        
            sitemap = getMultiAdapter((self.portal(), self.request),
                                       name='sitemap.xml.gz')
        except:
            print "can not adapter"
            return 
        alllink = sitemap.objects()
        links = []
#    import pdb
#    pdb.set_trace()
        for i in alllink:
            links.append(i['loc'])
        n = links[0].split('/')
        m = n[0] + '//' + n[2]
        h = m + '/'
        
        links.append(h)
        del n,m,h          
        import cPickle
        f = pkopenw('links.pkl')
        cPickle.dump(links,f)
        f.close()
        del cPickle,f
        
        # KSS specific calls
        core = self.getCommandSet('core')
        core.replaceInnerHTML('#flow-total', content)        
    
    def portal(self):
        return getToolByName(self.context, 'portal_url').getPortalObject()

class addaccessView(KSSView):     

    @kssaction
    def addaccess(self):
#        import pdb
#        pdb.set_trace()
        total = int(read('total'))        
        close('total')       
       
        total = total + 1
        content = str(total)
        del total
        write(content,'total')
        close('total')                    
        # KSS specific calls
        core = self.getCommandSet('core')
        core.replaceInnerHTML('#flow-total', content)

        
        
    
    

        
        


